package com.rocbrid.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rocbrid.common.R;
import com.rocbrid.pojo.Category;
import com.rocbrid.service.CategoryService;
import com.rocbrid.utils.ThreadLocalUtil;
import com.rocbrid.vo.req.CategoryAddVo;
import com.rocbrid.vo.req.CategoryUpdateVo;
import com.rocbrid.vo.resp.CategoryListVo;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Validated
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     * 删除文章分类
     * @param id
     * @return
     */

    @DeleteMapping
    public R deletCategoryById(@NotNull(message = "ID不能为空")  Integer id){
         categoryService.removeById(id);
        return R.ok();
    }

    /**
     * 更新文章分类
     * @param categoryUpdateVo
     * @return
     */
    @PutMapping
    public R updateCategory(@RequestBody CategoryUpdateVo categoryUpdateVo) {

        UpdateWrapper<Category> categoryUpdateWrapper = new UpdateWrapper<>();
        categoryUpdateWrapper.eq("id", categoryUpdateVo.getId())
                .set("category_name", categoryUpdateVo.getCategoryName())
                .set("category_alias", categoryUpdateVo.getCategoryAlias());
        categoryService.update(categoryUpdateWrapper);
        return R.ok();
    }

    /**
     * 获取文章分类详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public R getCategoryByID(@PathVariable("id") @NotNull Integer id) {
        QueryWrapper<Category> categoryVoQueryWrapper = new QueryWrapper<>();
        categoryVoQueryWrapper.eq("id", id);
        Category one = categoryService.getOne(categoryVoQueryWrapper);
        CategoryListVo CategoryVo = new CategoryListVo();
        BeanUtils.copyProperties(one, CategoryVo);
        return R.ok(CategoryVo);
    }

    /**
     * 新增成功
     *
     * @param categoryAddVo
     * @return
     */
    @PostMapping
    public R addCategory(@RequestBody @Valid CategoryAddVo categoryAddVo) {
        Map<String, Object> climas = ThreadLocalUtil.get();
        Integer id = (Integer) climas.get("id");
        categoryAddVo.setCreateUser(id);
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddVo, category);
        categoryService.save(category);
        return R.ok();
    }

    /**
     * 文章分类列表
     *
     * @return
     */

    @GetMapping
    public R getCategoryList() {
        Map<String, Object> climas = ThreadLocalUtil.get();
        Integer id = (Integer) climas.get("id");
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.ge("create_user", id);
        List<Category> list = categoryService.list(categoryQueryWrapper);
        List<CategoryListVo> collect = list.stream().map(s ->
                {
                    CategoryListVo listVo = new CategoryListVo();
                    BeanUtils.copyProperties(s, listVo);
                    return listVo;
                }
        ).collect(Collectors.toList());
        return R.ok(collect);
    }

}
